package com.xgpro.common.utils;


import com.xgpro.domain.rolemanage.entity.Department;
import com.xgpro.mapper.rolemanage.DepartmentMapper;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * @author tx
 * @version 1.0
 * @date 2019/11/4 23:13
 */
@Component
public class DeptmentUtil {
    @Resource
    private  DepartmentMapper departmentMapper;

    /**
     * 根据二级部门获取所有子部门id
     * @param secondDeptId
     * @return
     */
    public  Set<Integer> getChildDeptIds(Set<Integer> set,Integer secondDeptId){
        Example example = new Example(Department.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pid",secondDeptId+"");
        List<Department> departments = departmentMapper.selectByExample(example);
        if(departments!=null&&departments.size()>0){
            for (Department department : departments) {
                Integer id = department.getId();
                set.add(id);
                getChildDeptIds(set,id);
            }
        }
        set.add(secondDeptId);
        return set;
    }

}
